package com.gitee.controller;

import com.gitee.bo.CookbookBo;
import com.gitee.enums.CookbookTypeEnum;
import com.gitee.enums.ResponseEnum;
import com.gitee.pojo.Food;
import com.gitee.service.ICookbookService;
import com.gitee.service.IFoodService;
import com.gitee.vo.CookbookVo;
import com.gitee.vo.PageVo;
import com.gitee.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜谱管理
 */
@RestController
@RequestMapping("/cookbook")
public class CookbookController {

    @Autowired
    private ICookbookService cookbookService;

    @Autowired
    private IFoodService foodService;

    /**
     * 菜谱列表
     * @param eatTime
     * @param cookbookType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseVo<PageVo<CookbookVo>> list(String eatTime, Integer cookbookType, Integer pageNum, Integer pageSize) {
        eatTime = "null".equals(eatTime) ? null : eatTime;
        if (cookbookType != null) {
            cookbookType = CookbookTypeEnum.of(cookbookType).getCode();
        }
        PageVo<CookbookVo> cookbookVoPageVo = cookbookService.pageCookbookVos(eatTime, cookbookType, pageNum, pageSize);
        return ResponseVo.ok(cookbookVoPageVo);
    }

    /**
     * 全部菜品
     * @return
     */
    @GetMapping("/food")
    public ResponseVo<List<Food>> foodList() {
        List<Food> foodList = foodService.list();
        return ResponseVo.ok(foodList);
    }

    /**
     * 添加菜谱
     * @param cookbookBo
     * @return
     */
    @PostMapping
    public ResponseVo<String> add(@RequestBody CookbookBo cookbookBo) {
        cookbookService.addCookbook(cookbookBo);
        return ResponseVo.ok(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 更新菜谱
     * @param cookbookBo
     * @param cookbookId
     * @return
     */
    @PutMapping("/{cookbookId}")
    public ResponseVo<String> update(@RequestBody CookbookBo cookbookBo, @PathVariable Integer cookbookId) {
        cookbookService.updateCookbook(cookbookBo, cookbookId);
        return ResponseVo.ok(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 删除菜谱
     * @param cookbookId
     * @return
     */
    @DeleteMapping("/{cookbookId}")
    public ResponseVo<String> delete(@PathVariable Integer cookbookId) {
        cookbookService.deleteCookbook(cookbookId);
        return ResponseVo.ok(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 批量删除菜谱
     * @param cookbookIds
     * @return
     */
    @DeleteMapping("/batch")
    public ResponseVo<String> delete(@RequestBody Integer[] cookbookIds) {
        cookbookService.deleteCookbookBatch(cookbookIds);
        return ResponseVo.ok(ResponseEnum.DELETE_SUCCESS);
    }
}
